﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.ActiveDirectory.Model;
using Domain.Context;
using Domain.Logging;
using Domain.Provider;
using WebUI.DataModel;

namespace WebUI.Controllers
{
    public class StoreController : NMCToolControllerBase
    {
        private IDataProvider<NMCTerritory> _nmcTerritoryRepo;
        private IDataProvider<Store> _storeRepo;
        private IDataProvider<User> _nmcRepo;
        private LogService _logger;

        public StoreController(LogService logger, IDataProvider<Store> storeRepo,IDataProvider<NMCTerritory> nmcTerritoryRepo, IDataProvider<User> nmcRepo) : base(logger)
        {
            _storeRepo = storeRepo;
            _nmcTerritoryRepo = nmcTerritoryRepo;
           _nmcRepo = nmcRepo;
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            try
            {
                // Fill the view bag 
                ViewBag.NMCTerritories = _nmcTerritoryRepo.Entities
                    .Select(territory =>
                    {
                        string value = "";
                        if (territory.NMC_Area != null)
                            value = territory.NMC_Area;
                        return value;
                    })
                    .Where(value => !string.IsNullOrWhiteSpace(value));
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public ActionResult StoresGrid()
        {
            try
            {
                return PartialView("_StoresGrid", GetModels());
            }
            catch (Exception e)
            {
                LogException(this, e);
                return RedirectToAction("error", "DatabaseError");
            }

        }

        public ActionResult Save(StoreModel store)
        {
            store.Store.NMC_UID = (from territory in _nmcTerritoryRepo.Entities where
                                  territory.NMC_Area == store.TerritoryName
                                  select territory.NMC_UID).First();
            try
            {
                _storeRepo.SaveEntity(store.Store, User);
                return PartialView("_StoresGrid", GetModels());
            }
            catch (Exception e)
            {
                LogException(this, e);
                return RedirectToAction("error", "DatabaseError");
            }
        }

        public ActionResult Index()
        {
            try
            {
                return View(GetModels());
            }
            catch (Exception e)
            {
                LogException(this, e);
                return RedirectToAction("error", "DatabaseError");
            }
        }

        public IEnumerable<StoreModel> GetModels()
        {
            var stores = _storeRepo.Entities;
            var territories = _nmcTerritoryRepo.Entities;

            IEnumerable<StoreModel> storeModels =
                stores.Join(territories, key => key.NMC_UID,
                territory => territory.NMC_UID,
                (outerkey, innerkey) => new StoreModel() { Store = outerkey, NMCTerritory = innerkey });

            return storeModels;
        }
    }
}
